package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVo;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


//@RestController 组合注解：@Controller + @ResponseBody，默认返回json数据
//@Controller
@Slf4j
@RestController
@Tag(name = "01-咨询管理模块")
@RequestMapping("v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
//    @ResponseBody
    @Operation(summary = "01-添加咨询")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
        @Parameter(name = "title",description = "咨询标题"),
        @Parameter(name = "type",description = "咨询类型"),
        @Parameter(name = "content",description = "咨询内容"),
        @Parameter(name = "status",description = "咨询状态"),
            @Parameter(name = "noticeAddParam",hidden = true)
    })
    public JsonResult add(NoticeAddParam noticeAddParam){
        log.debug("添加咨询："+noticeAddParam);
        Notice notice = new Notice();
//        notice.setTitle(noticeAddParam.getTitle());
//        notice.setContent(noticeAddParam.getContent());
//        notice.setType(noticeAddParam.getType());
//        notice.setStatus(noticeAddParam.getStatus());
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @Operation(summary = "02-查询所有咨询")
    @ApiOperationSupport(order = 20)
    public JsonResult selectAllNotices(NoticeListQuery noticeListQuery){
        log.debug("输入："+noticeListQuery);
        return JsonResult.ok(noticeMapper.listNotice(noticeListQuery));
    }

    @GetMapping("detail")
    @Operation(summary = "03-查询咨询详情")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id",description = "咨询id",required = true,example = "1")
    //@Parameter(name = "name",description = "咨询name",required = true,hidden = true)
    public JsonResult detail(Long id,String name){
        log.debug("输入："+id);
        return JsonResult.ok();
    }
    @DeleteMapping("del")
    @Operation(summary = "04-删除咨询")
    @ApiOperationSupport(order = 40)
    public JsonResult delete(Long id){
        log.debug("输入："+id);
        int rows = noticeMapper.deleteById(id);
        if (rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    @Operation(summary = "05-修改咨询")
    @ApiOperationSupport(order = 50)
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("输入："+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int rows = noticeMapper.updateNotice(notice);
        if (rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }
}
